package com.enbatis.mybatisplugs.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.enbatis.mybatisplugs.base.Account;
import com.enbatis.mybatisplugs.base.BaseServiceImpl;

import com.enbatis.mybatisplugs.commons.sql.Wrapper;
//import com.enbatis.mybatisplugs.component.WebSocket;
import com.enbatis.mybatisplugs.entity.SysMsg;
import com.enbatis.mybatisplugs.entity.SysMsgRelUser;
import com.enbatis.mybatisplugs.entity.SysUser;
import com.enbatis.mybatisplugs.entity.dto.SysMsgDTO;
import com.enbatis.mybatisplugs.mapper.SysMsgMapper;
import com.enbatis.mybatisplugs.service.SysMsgRelUserService;
import com.enbatis.mybatisplugs.service.SysMsgService;
import com.enbatis.mybatisplugs.service.SysUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* 描述：
* @author wangweidong
* @date 2019-12-17 16:18:11
*/

@Service
public class SysMsgServiceImpl extends BaseServiceImpl<SysMsgMapper,SysMsg > implements SysMsgService {


          @Autowired
          private SysUserService sysUserService;

        @Autowired
        private SysMsgRelUserService sysMsgRelUserService;

//        @Autowired
//        private WebSocket webSocket;

        public  void  sendMsg(String msg, List<Long> userIds,Account account){

            SysMsg sysMsg = new SysMsg();
            sysMsg.setName(msg);
            sysMsg.setSender(account.getUsername());
            sysMsg.setSenderId(account.getId());
            sysMsg.setCtime(new Date());
            sysMsg.setDeleted(0);
            sysMsg.setCompanyId(account.getCompanyId());

            SysMsgDTO dto = new SysMsgDTO();
            dto.setSysMsg(sysMsg);
            dto.setUserIds(userIds);



            if (insert(sysMsg)!=null){
                List<SysMsgRelUser> saveRel=new ArrayList<>();
                if (CollectionUtils.isEmpty(userIds)){

                    List<SysUser> listAll = sysUserService.list(new Wrapper<>(new SysUser()).eq("company_id",sysMsg.getCompanyId()));
                    for (SysUser user:listAll) {
                        SysMsgRelUser relUser = new SysMsgRelUser();
                        relUser.setCompanyId(sysMsg.getCompanyId());
                        relUser.setDeleted(0);
                        relUser.setMsgId(sysMsg.getId());
                        relUser.setUserId(user.getId());
                        relUser.setReaded(0);
                        saveRel.add(relUser);
                    }

                }else{
                    for (Long usd:userIds) {
                        SysMsgRelUser relUser = new SysMsgRelUser();
                        relUser.setCompanyId(sysMsg.getCompanyId());
                        relUser.setDeleted(0);
                        relUser.setMsgId(sysMsg.getId());
                        relUser.setUserId(usd);
                        relUser.setReaded(0);
                        saveRel.add(relUser);

                    }
                }
                if (CollectionUtils.isNotEmpty(saveRel)){
                    if (sysMsgRelUserService.saveBatch(saveRel)==saveRel.size()){
//                        for (SysMsgRelUser msgRelUser:saveRel) {
//                            webSocket.sendOneMessage(String.valueOf(msgRelUser.getUserId()),sysMsg.getName());
//                        }
                    }
                }
            }
        }

}
